package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.Merchant;
import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.InterfaceService;
import cn.gjculture.shopping.api.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/merchant")
public class MerchantController {

//    @Value("${register.successUrl}")
    private String registerSuccessUrl;

    @Autowired
    MerchantService merchantService;

    @Autowired
    InterfaceService interfaceService;


    @PostMapping
    public Return save(Merchant mer){
        Return ret=new Return();
        int i = merchantService.save(mer, ret);
        if(i>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @GetMapping(value = "/registerBack")
    public String registerBack(){
        return "注册成功";
    }

    @GetMapping
    public Return<List<Merchant>> query(PageInfo pageInfo){
        Return<List<Merchant>> ret=new Return<>();
        List<Merchant> merchants=merchantService.query();
        if(!CollectionUtils.isEmpty(merchants)){
            ret.setData(merchants);
            ret.getCommon().setRet(true);
        }
        return ret;
    }
    @PutMapping
    public Return update(Merchant merchant){
        Return ret=new Return();
        if(merchantService.update(merchant,ret)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }
    @PostMapping(value = "/registerAgain")
    public Return registerAgain(Merchant merchant){
        Return ret=new Return();
        if(merchantService.registerAgain(merchant,ret)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @GetMapping(value = "/system")
    public Return<List<Merchant>> querySystem(){
        Return<List<Merchant>> ret=new Return<>();
        List<Merchant> list=merchantService.querySystem();
        ret.setData(list);
        if(!CollectionUtils.isEmpty(list)) ret.getCommon().setRet(true);
        return ret;
    }



}
